package com.it2297.view;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import com.it2297.controller.ProductController;
import com.it2297.main.UserApp;
import com.it2297.model.Product;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.Font;

public class ProductDetailDialog extends JDialog {
	private final ProductController pc = new ProductController(
			ProductDetailDialog.this);
	private final JPanel contentPanel = new JPanel();
	private final Product product = new Product();
	private final JLabel lbl_img = new JLabel("");
	private final JTextField lbl_title = new JTextField();
	private final JTextArea textArea = new JTextArea();
	private final JLabel lblNewLabel = new JLabel("Price:");
	private final JTextField lbl_price = new JTextField();
	private final ProductCommentView commentView = new ProductCommentView();
	private final JLabel lblNewLabel_1 = new JLabel("Quantity: ");
	private final JTextField lbl_Quantity = new JTextField("");
	private final JLabel lblNewLabel_2 = new JLabel("Type:");
	private final JTextField lblProductType = new JTextField("");
	public String productname = getProductName();
	private final JButton btnAddCart = new JButton("Add to Cart");
	private int ProductID;
	public ProductDetailDialog() {

		this.setLocationRelativeTo(null);
		this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		setBounds(100, 100, 800, 500);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		lbl_img.setBounds(0, 0, 201, 193);

		contentPanel.add(lbl_img);
		lbl_title.setEditable(false);
		lbl_title.setFont(new Font("Tahoma", Font.PLAIN, 30));
		lbl_title.setBounds(291, 0, 252, 44);

		contentPanel.add(lbl_title);
		textArea.setEditable(false);
		textArea.setBounds(415, 55, 230, 126);

		contentPanel.add(textArea);
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblNewLabel.setBounds(211, 55, 53, 32);

		contentPanel.add(lblNewLabel);
		lbl_price.setEditable(false);
		lbl_price.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lbl_price.setBounds(291, 55, 107, 32);

		contentPanel.add(lbl_price);

		JSeparator sep = new JSeparator();
		sep.setBounds(0, 192, 774, 10);
		sep.setForeground(Color.black);
		contentPanel.add(sep);

		commentView.setBounds(0, 192, 774, 248);
		contentPanel.add(commentView);
		lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblNewLabel_1.setBounds(211, 91, 88, 32);

		contentPanel.add(lblNewLabel_1);
		lbl_Quantity.setEditable(false);
		lbl_Quantity.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lbl_Quantity.setBounds(291, 95, 107, 23);

		contentPanel.add(lbl_Quantity);
		lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblNewLabel_2.setBounds(211, 134, 53, 26);

		contentPanel.add(lblNewLabel_2);
		lblProductType.setEditable(false);
		lblProductType.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblProductType.setBounds(291, 134, 107, 26);

		contentPanel.add(lblProductType);
		if(UserApp.getCurrentUser().getRole().equals("CUSTOMER")){
		btnAddCart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Product product = new Product();
				product.setProductid(getProductID());
				product.setProdName(getProductName());
				product.setPrice(getProductPrice());

				pc.addOrderList(product);
				
				ShoppingCartView.getScrollpane().getViewport().removeAll();
				pc.addProducts();
				close();
			}
		});
		btnAddCart.setBounds(655, 134, 102, 32);
		}
		contentPanel.add(btnAddCart);

	}
	
	public void setProductID(int num){
		ProductID = num;
	}
	public int getProductID(){
		return ProductID;
	}
	public void setProductName(String name) {
		lbl_title.setText(name);
	}

	public String getProductName() {
		return lbl_title.getText();
	}

	public void setProductDesc(String desc) {
		textArea.setText(desc);
	}

	public void setProductPrice(double price) {
		lbl_price.setText(Double.toString(price));
	}

	public double getProductPrice() {
		return Double.parseDouble(lbl_price.getText());
	}

	public void setProductType(String type) {
		lbl_price.setText(type);
	}

	public void setProductQuantity(int quantity) {
		lbl_Quantity.setText(Integer.toString(quantity));
	}

	public void setCategory(String category) {
		lblProductType.setText(category);
	}

	public void setImg(String name) {
		Image bg = Toolkit
				.getDefaultToolkit()
				.createImage(
						System.getProperty("user.dir") + "/img/product-img/"
								+ name + ".png")
				.getScaledInstance(158, 120, Image.SCALE_DEFAULT);
		lbl_img.setIcon(new ImageIcon(bg));
	}

	public void close() {
		this.dispose();
	}
	public void setBtn(){
		btnAddCart.setVisible(false);
	}
	
}
